package rattlesnake.callback.util;

import com.sun.org.apache.regexp.internal.RE;
import rattlesnake.callback.modle.dto.HttpRequestWrapper;

import java.util.*;

/**
 * @Package: rattlesnake.callback.util
 * @Description: 等待重试的集合
 * @author: liuxin
 * @date: 17/4/14 下午3:32
 */
public class ChannelSet {
    private final static Set<HttpRequestWrapper> RequestSet=new HashSet<HttpRequestWrapper>();

    public void addChannel(HttpRequestWrapper request){
        if (RequestSet.contains(request)){
            return;
        }
        check();
        RequestSet.add(request);
    }

    public Set<HttpRequestWrapper> getChannelSet(){
        return RequestSet;
    }


    public void removeChannel(HttpRequestWrapper request){
        RequestSet.remove(request);
    }
    //TODO 失败就进行重试,三次是失败就返回失败
    public void check(){
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                Iterator<HttpRequestWrapper>iterator= RequestSet.iterator();
                if (iterator.hasNext()){
                    HttpRequestWrapper httpRequestWrapper=iterator.next();
                    HttpAsyncUtil.doExcutes(httpRequestWrapper);
                }
            }
        });
        thread.setDaemon(true);
    }

}
